// Drummer Server
// by Andrea Bianchi
// drummer.alsoplantsfly.com

/*
Copyright (C) 2009, Andrea Bianchi
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */


import processing.net.*;


class DrummerNetwork
{

  private Server myServer_;        
  private boolean initialized_;
  private PApplet hnd_;

  DrummerNetwork (PApplet hnd)
  {
    initialized_= false;
    hnd_= hnd;
  }

  void startNetwork(int port)
  {
    if (initialized_) return;
    myServer_ = new Server (hnd_, port);
    initialized_= true;
  }

  boolean isInitialized(){ 
    return initialized_;
  }

  void stopNetwork()
  {
    initialized_= false;
    myServer_.stop();
    myServer_ = null;  
  }
 
 void toggleNetwork()
 {
  if (isInitialized()) 
    stopNetwork(); 
  else  
    new PortForm();
 }


  void update ()
  {  
    if (initialized_)
    {
      // Get the next available client
      Client thisClient = myServer_.available();
      // If the client is not null, and says something, display what it said
      if (thisClient !=null) {
        String whatClientSaid = thisClient.readString();
        if (whatClientSaid != null) {
           String parts[] = split(whatClientSaid, ';');
          playSound (thisClient.ip(), parts[0]);
        }
      }
    }  
  }
}



